﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RM2012Server.Model;
using System.Data;
using RM2012Server.Business;
using DevComponents.DotNetBar.Controls;
using System.Windows.Forms;

namespace RM2012Server.Controller
{
    public class CategoryCtrl
    {
        CategoryDAO m_CategoryDAO = new CategoryDAO();

        public void LoadToComboBox(ComboBoxEx cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "CateName";
            cmb.ValueMember = "CateId";
        }

        public void LoadToDataGridViewComboBoxColumn(DataGridViewComboBoxColumn cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "CateName";
            cmb.ValueMember = "CateId";
            cmb.DataPropertyName = "CateId";
            cmb.HeaderText = "Danh mục";
        }

        public void LoadToDataGridViewComboBoxExColumn(DataGridViewComboBoxExColumn cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "CateName";
            cmb.ValueMember = "CateId";
            cmb.DataPropertyName = "CateId";
            cmb.HeaderText = "Danh mục";
        }

        public void Show(DataGridViewX dgv, BindingNavigator bn)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = GetAll();
            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public void Show(DataGridViewX dgv,
                         BindingNavigator bn,
                         TextBoxX txtCateId,
                         TextBoxX txtCateName,
                         TextBoxX txtRemark)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = GetAll();

            txtCateId.DataBindings.Clear();
            txtCateId.DataBindings.Add("Text", bs, "CateId");

            txtCateName.DataBindings.Clear();
            txtCateName.DataBindings.Add("Text", bs, "CateName");

            txtRemark.DataBindings.Clear();
            txtRemark.DataBindings.Add("Text", bs, "Remark");

            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public void ShowWithSources(DataGridViewX dgv,
                         BindingNavigator bn,
                         TextBoxX txtCateId,
                         TextBoxX txtCateName,
                         TextBoxX txtRemark,
                         DataTable dtSources)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = dtSources;

            txtCateId.DataBindings.Clear();
            txtCateId.DataBindings.Add("Text", bs, "CateId");

            txtCateName.DataBindings.Clear();
            txtCateName.DataBindings.Add("Text", bs, "CateName");

            txtRemark.DataBindings.Clear();
            txtRemark.DataBindings.Add("Text", bs, "Remark");

            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public DataTable GetAll()
        {
            return m_CategoryDAO.GetAll();
        }

        public DataTable GetById(String cateId)
        {
            return m_CategoryDAO.GetById(cateId);
        }

        public DataTable GetByLikeId(String cateId)
        {
            return m_CategoryDAO.GetByLikeId(cateId);
        }

        public DataTable GetByName(String cateName)
        {
            return m_CategoryDAO.GetByName(cateName);
        }

        public int Add(CategoryInfo info)
        {
            return m_CategoryDAO.Add(info);
        }

        public int Update(CategoryInfo info)
        {
            return m_CategoryDAO.Update(info);
        }

        public int Delete(String cateId)
        {
            return m_CategoryDAO.Delete(cateId);
        }

        public DataRow AddNewRow()
        {
            return m_CategoryDAO.AddNewRow();
        }

        public void AddNewCate(DataRow m_Row)
        {
            m_CategoryDAO.AddNewCate(m_Row);
        }

        public bool SaveData()
        {
            return m_CategoryDAO.SaveData();
        }
    }
}
